//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ruoyi.system.controller;


import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysOrg;
import com.ruoyi.system.service.ISysOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(
        tags = {"供电单位"}
)
@RestController
@RequestMapping({"/org"})
public class SysOrgController extends BaseController {
    @Autowired
    private ISysOrgService sysOrgService;

    public SysOrgController() {
    }

    @RequiresPermissions({"system:org:list"})
    @ApiOperation("查询所有供电信息基本信息列表")
    @GetMapping({"/list"})
    public AjaxResult list(SysOrg sysOrg) {
        List<SysOrg> list = this.sysOrgService.selectSysOrgList(sysOrg);
        return AjaxResult.success(list);
    }

    @GetMapping({"/treeselect"})
    public AjaxResult treeselectOrg(SysOrg org) {
        List<SysOrg> orgs = this.sysOrgService.selectSysOrgList(org);
        return AjaxResult.success(this.sysOrgService.buildOrgTreeSelect(orgs));
    }

    @GetMapping({"/treeOrgList"})
    public AjaxResult treeOrgList() {
        Long userId = SecurityUtils.getUserId();
        SysOrg org = this.sysOrgService.selectOrgNoByUserId(userId);
        List<SysOrg> orgs = this.sysOrgService.selectSysOrgList(org);
        return AjaxResult.success(this.sysOrgService.buildOrgTreeSelect(orgs));
    }

    @ApiOperation("导出供电信息基本信息列表")
    @RequiresPermissions({"system:org:export"})
    @Log(
            title = "供电单位基本信息",
            businessType = BusinessType.EXPORT
    )
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, SysOrg sysOrg) {
        List<SysOrg> list = this.sysOrgService.selectSysOrgList(sysOrg);
        ExcelUtil<SysOrg> util = new ExcelUtil(SysOrg.class);
        util.exportExcel(response, list, "供电单位基本信息数据");
    }

    @ApiOperation("获取供电单位基本信息详细信息")
    @RequiresPermissions({"system:org:query"})
    @GetMapping({"/getInfo"})
    public AjaxResult getInfo(String orgNo) {
        return AjaxResult.success(this.sysOrgService.selectSysOrgByOrgNo(orgNo));
    }

    @ApiOperation("新增供电单位基本信息")
    @RequiresPermissions({"system:org:add"})
    @Log(
            title = "供电单位基本信息",
            businessType = BusinessType.INSERT
    )
    @PostMapping({"/add"})
    public AjaxResult add(@Validated @RequestBody SysOrg org) {
        if ("1".equals(this.sysOrgService.checkOrgNameUnique(org))) {
            return AjaxResult.error("新增供电单位'" + org.getOrgName() + "'失败，供电单位名称已存在");
        } else if ("1".equals(this.sysOrgService.checkOrgNoUnique(org))) {
            return AjaxResult.error("新增供电单位失败，供电单位编码重复");
        } else if (org.getOrgType().equals(this.sysOrgService.checkOrgType(org.getpOrgNo()))) {
            return AjaxResult.error("新增供电单位失败，单位类型与上级供电单位一致");
        } else if (Integer.parseInt(org.getOrgType()) < Integer.parseInt(this.sysOrgService.checkOrgType(org.getpOrgNo()))) {
            return AjaxResult.error("新增供电单位失败，单位类型不可高于上级供电单位");
        } else {
            org.setCreateBy(SecurityUtils.getUsername());
            return this.toAjax(this.sysOrgService.insertSysOrg(org));
        }
    }

    @ApiOperation("修改供电单位基本信息")
    @RequiresPermissions({"system:org:edit"})
    @Log(
            title = "供电单位基本信息",
            businessType = BusinessType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@RequestBody SysOrg org) {
        String orgNo = org.getOrgNo();
        this.sysOrgService.checkOrgDataScope(orgNo);
        if ("1".equals(this.sysOrgService.checkOrgNameUnique(org))) {
            return AjaxResult.error("修改供电单位'" + org.getOrgName() + "'失败，供电单位名称已存在");
        } else if (org.getpOrgNo().equals(orgNo)) {
            return AjaxResult.error("修改供电单位'" + org.getOrgName() + "'失败，上级部门不能是自己");
        } else {
            org.setUpdateBy(SecurityUtils.getUsername());
            return this.toAjax(this.sysOrgService.updateSysOrg(org));
        }
    }

    @ApiOperation("删除供电单位基本信息")
    @RequiresPermissions({"system:org:remove"})
    @Log(
            title = "供电单位基本信息",
            businessType = BusinessType.DELETE
    )
    @PostMapping({"/delete"})
    public AjaxResult remove(@RequestBody SysOrg org) {
        if (this.sysOrgService.hasChildByOrgNo(org.getOrgNo())) {
            return AjaxResult.error("存在下级供电单位，不允许删除");
        } else {
            this.sysOrgService.checkOrgDataScope(org.getOrgNo());
            return this.toAjax(this.sysOrgService.deleteSysOrgByOrgNo(org.getOrgNo()));
        }
    }
}
